package com.zoshine.zzg.controller;

import com.github.pagehelper.PageInfo;
import com.zoshine.zzg.base.BaseResponse;
import com.zoshine.zzg.exception.AppException;
import com.zoshine.zzg.exception.ErrorMessage;
import com.zoshine.zzg.pojo.User;
import com.zoshine.zzg.pojo.pojostatus.CommonStatus;
import com.zoshine.zzg.respons.AppResponse;
import com.zoshine.zzg.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 用户Controller
 *
 * @author 田沛凌
 * @time 2017年6月29日 下午8:24:03
 */
@RestController
@RequestMapping(value = "/user", method = RequestMethod.POST)
public class UserController {

    private UserService userService;

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }


    /**
     * 根据ID获取用户信息
     *
     * @param id 用户编号
     */
    @RequestMapping("/getUser")
    public BaseResponse getUser(Long id) {
        User user = userService.get(id);
        return new AppResponse(user);
    }

    /**
     * 根据ID删除用户信息
     *
     * @param id 用户编号
     */
    @RequestMapping("/delUser")
    public BaseResponse delUser(Long id) {
        if (id == 0L) {
            throw new AppException(ErrorMessage.SUPER_ADMIN_CAN_NOT_DELETE);
        }
        userService.del(id);
        return AppResponse.OK;
    }

    /**
     * 新增用户
     *
     * @param user 用户信息
     */
    @RequestMapping("/addUser")
    public BaseResponse addUser(User user) {
        userService.add(user);
        return AppResponse.OK;
    }

    /**
     * 修改用户
     *
     * @param user 用户信息
     */
    @RequestMapping("/editUser")
    public BaseResponse editUser(User user) {
        userService.edit(user);
        return AppResponse.OK;
    }

    /**
     * 获取用户分页列表
     *
     * @param user 查询参数
     */
    @RequestMapping("/pageUser")
    public BaseResponse pageUser(User user) {
        List<User> listUser = userService.list(user);
        return new AppResponse(new PageInfo<>(listUser));
    }

    /**
     * 获取用户状态列表
     *
     * @return 用户状态列表
     */
    @RequestMapping("/listUserStatus")
    public BaseResponse listRoleStatus() {
//        List<Map<String, Object>> list = new ArrayList<>();
//        for (CommonStatus commonStatus : CommonStatus.values()) {
//            Map<String, Object> map = new HashMap<>();
//            map.put("id", commonStatus.getValue());
//            map.put("text", CommonStatus.getDesc(commonStatus.getValue()));
//            list.add(map);
//        }
        return new AppResponse(CommonStatus.getList());
    }
}
